package com.calvin.study.ocr3.context;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import com.calvin.study.ocr3.strategy.IStrategy;

import cn.hutool.extra.spring.SpringUtil;
@Component
public class StrategyContext implements InitializingBean {

	private static Map<String, IStrategy> registerMap = new HashMap<>();

	// 注册策略
	public static void registerStrategy(String rewardType, IStrategy strategy) {
		registerMap.putIfAbsent(rewardType, strategy);
	}

	// 获取策略
	public static IStrategy getStrategy(String rewardType) {
		return registerMap.get(rewardType);
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		// 将实现该（IStrategy）接口的Bean实例化，然后注册到内存中。
		registerMap = SpringUtil.getBeansOfType(IStrategy.class);
	}

}
